
#include "NumKeyBoard.h"
#include "assert.h"
#include <qpushbutton.h>
#include <qstring.h>
#include <qlineedit.h>

#define MAX_NUM_LEN 10

NumKeyBoard::NumKeyBoard( QWidget * parent, const char * name, bool modal, WFlags f) : 
	KeyBoardDialog(parent, name, modal, f)
{

	connect(okButton, SIGNAL(clicked()), this, SLOT(okClicked()));
	connect(cancelButton, SIGNAL(clicked()), this, SLOT(cancelClicked()));
	connect(backButton, SIGNAL(clicked()), this, SLOT(backClicked()));
	connect(clearButton, SIGNAL(clicked()), this, SLOT(clearClicked()));

	connect(zeroButton, SIGNAL(clicked()), this, SLOT(zeroClicked()));
	connect(oneButton, SIGNAL(clicked()), this, SLOT(oneClicked()));
	connect(twoButton, SIGNAL(clicked()), this, SLOT(twoClicked()));

	connect(threeButton, SIGNAL(clicked()), this, SLOT(threeClicked()));
	connect(fourButton, SIGNAL(clicked()), this, SLOT(fourClicked()));
	connect(fiveButton, SIGNAL(clicked()), this, SLOT(fiveClicked()));
	connect(sixButton, SIGNAL(clicked()), this, SLOT(sixClicked()));
	connect(sevenButton, SIGNAL(clicked()), this, SLOT(sevenClicked()));
	connect(eightButton, SIGNAL(clicked()), this, SLOT(eightClicked()));
	connect(nineButton, SIGNAL(clicked()), this, SLOT(nineClicked()));





	connect(this, SIGNAL(doneAndGetValue(int)), this, SLOT(done(int)));
}


NumKeyBoard::~NumKeyBoard()
{
}

void NumKeyBoard::okClicked()
{

	int v = 0;
	bool isOk = false;
	QString str = numEdit->text();

	if(str.isEmpty())
	{
		emit doneAndGetValue(0);
		return;
	}
	v = str.toInt(&isOk,10);
	assert(isOk);


	emit doneAndGetValue(v);
}


void NumKeyBoard::cancelClicked()
{
	emit doneAndGetValue(0);
}

void NumKeyBoard::clearClicked()
{

	numEdit->setText(tr(""));
}


void NumKeyBoard::backClicked()
{
	QString str = numEdit->text();
	if(!str.isEmpty())
	{
		str.remove(str.length() - 1, 1);
	}
	numEdit->setText(str);
}


void NumKeyBoard::zeroClicked()
{
	appendNumberText(tr("0"));


}

void NumKeyBoard::oneClicked()
{

	appendNumberText(tr("1"));
}

void NumKeyBoard::twoClicked()
{
	appendNumberText(tr("2"));
}

void NumKeyBoard::threeClicked()
{
	appendNumberText(tr("3"));
}

void NumKeyBoard::fourClicked()
{
	appendNumberText(tr("4"));
}

void NumKeyBoard::fiveClicked()
{
	appendNumberText(tr("5"));
}

void NumKeyBoard::sixClicked()
{
	appendNumberText(tr("6"));
}

void NumKeyBoard::sevenClicked()
{
	appendNumberText(tr("7"));
}

void NumKeyBoard::eightClicked()
{
	appendNumberText(tr("8"));
}

void NumKeyBoard::nineClicked()
{
	appendNumberText(tr("9"));
}




void NumKeyBoard::appendNumberText(const QString& s)
{
	QString str = numEdit->text();
	if(str.length() < MAX_NUM_LEN )
	{
		str.append(s);
		numEdit->setText(str);
	}


}
